import {
  createRouter,
  createWebHashHistory
} from 'vue-router'
import login from './modules/login'
import vip from './modules/vip'

const routes = [{
    path: '/',
    component: () => import('@/view/index.vue'),
    children: [{
        path: '',
        component: () => import('@/view/home/home.vue'),
      },

    ]
  },
  {
    path: '/search',
    component: () => import('@/view/index.vue'),
    meta: {
      title: '搜索结果',
      showInbreadcrumb: true
    },
    children: [{
      path: ':name',
      meta: {
        title: '手机',
        showInbreadcrumb: true
      },
      props: true,
      component: () => import('@/view/search/search.vue'),
    }]
  },
  {
    path: '/category/:id',
    component: () => import('@/view/index.vue'),
    children: [{
      path: '',
      component: () => import('@/view/category/category.vue')
    }]
  },
  {
    path: '/filter/:id',
    component: () => import('@/view/index.vue'),
    children: [{
      path: '',
      component: () => import('@/view/category/filter.vue')
    }]
  },
  {
    path: '/shopping-cart',
    component: () => import('@/view/index.vue'),
    children: [{
      path: '',
      component: () => import('@/view/shopping-cart/shopping-cart.vue')
    }]
  },
  ...vip
  ,
  // 登录相关
  {
    path: '/registeredAndLogin',
    component: () => import('@/view/login/index.vue'),
    meta: {
      requireAuth: true
    },
    redirect: '/registeredAndLogin/login',
    children: [
      ...login
    ]
  }
]


const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from) => {
  const token = localStorage.getItem('token')

  // console.log(to,from);
  // to.meta.title = to.params.name

  if (to.path == '/search') {
    return false
  }


  if (token) {
    if (to.meta.requireAuth) {
      return {
        path: from.path
      }
    }
  } else {
    if (to.meta.requireUs) {
      return {
        path: '/registeredAndLogin/login'
      }
    }
  }

})

export default router

export {
  routes
}